import com.mysql.jdbc.NotUpdatable;

import java.util.Scanner;
import java.util.concurrent.ThreadLocalRandom;

public class testThread {

    static class waitTask implements Runnable{
        private Object locker;
        public waitTask(Object locker){
            this.locker = locker;
        }

        public void run(){
            synchronized (locker){
                while(true){
                    try{
                        System.out.println("wait开始");
                        locker.wait();
                        System.out.println("wait结束");
                    }catch (InterruptedException e){
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    static class NotifyTask implements Runnable{

        private Object locker;
        public NotifyTask(Object locker){
            this.locker = locker;
        }

        public void run(){
            synchronized (locker){
                System.out.println("notify 开始");
                locker.notifyAll();
                //locker.notify();
                System.out.println("notify 结束");
            }
        }
    }

    public static void main10(String[] args) throws InterruptedException {
        Object locker = new Object();
      Thread t1 = new Thread(new waitTask(locker));
      Thread t3 = new Thread(new waitTask(locker));
      Thread t4 = new Thread(new waitTask(locker));
      Thread t2 = new Thread(new NotifyTask(locker));
      t1.start();
      t3.start();
      t4.start();
      Thread.sleep(1000);
      t2.start();
    }



    private static class Count{
        public int flag = 0;
    }
    public static void main9(String[] args) {
        Count count = new Count();
        Thread t1 = new Thread(()->{
            while(true){
                synchronized (count){
                    if(count.flag != 0)
                        break;
                }
            }
            System.out.println("循环结束");
        },"t1");
        Thread t2 =  new Thread(()->{
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入一个整数");
            count.flag = scanner.nextInt();
        },"t1");
        t1.start();
        t2.start();

    }

    public static void main8(String[] args) {
        final Object object = new Object();
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (object){
                    while (true) {
                    try {
//                        Thread.sleep(1000);
                        object.wait();
                   } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
            }
        }, "t1");
        t1.start();

        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (object){
                    System.out.println("hehe");
                }
            }
        }, "t2");
        t2.start();

    }

    public static void main4(String[] args) throws InterruptedException {
        Thread t = new Thread(() -> {
              for (int i = 0; i < 1000_0000; i++) {
            }
      }, "李四");
        System.out.println(t.getName() + ": " + t.getState());
        System.out.println(t.getName() + ": " + t.isAlive());
        t.start();
        while (t.isAlive()) {
              System.out.println(t.getName() + ": " + t.getState());
            System.out.println(t.getName() + ": " + t.isAlive());
      }
        System.out.println(t.getName() + ": " + t.isAlive());;
        System.out.println(t.getName() + ": " + t.getState());
    }

    public static void main3(String[] args) throws InterruptedException {
        Runnable target = ()->{
            for (int i = 0; i < 10; i++) {
                try{
                    System.out.println(Thread.currentThread().getName());
                    Thread.sleep(1000);
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
            }
            System.out.println(Thread.currentThread().getName()+":我结束了");
        };

        Thread thread1 = new Thread(target, "李四");
        Thread thread2 = new Thread(target, "王五");
        System.out.println("先让李四开始工作");
        thread1.start();
//        thread1.join();
        System.out.println("李四工作结束了，让王五开始工作");
        thread2.start();
//        thread2.join();
        System.out.println("王五工作结束了");

    }



    private static class MyRunnable implements Runnable{
        @Override
        public void run() {
            for (int i = 0; i < 100; i++) {
                System.out.println(Thread.currentThread().isInterrupted());
//                try {
//                    Thread.sleep(500);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
            }
        }
    }

    public static void main6(String[] args) throws InterruptedException {
        MyRunnable target = new MyRunnable();
        Thread thread = new Thread(target, "李四");
        thread.start();
        Thread.sleep(1);
        for (int i = 0; i < 10; i++) {
            thread.interrupt();
//            Thread.sleep(1);
        }
//        thread.interrupt();
        System.out.println("结束了");
    }





    private static class MyRunnable1 implements Runnable{
        //public volatile boolean isQuit = false;
        private int num = 0;
        @Override
        public void run() {
//            while(!isQuit){
            while(!Thread.currentThread().isInterrupted()){
                System.out.println(Thread.currentThread().getName()+"： 别管我，我忙着转账呢！");
                try{
                    Thread.sleep(1000);
                    System.out.println("第"+num+"次转账");
                    num++;
                }catch(InterruptedException e){
                    e.printStackTrace();
                    System.out.println(Thread.currentThread().getName() + "有内鬼，终止交易");
//                    break;
                    //  注意这里的break  以及为什么要 sleep要放在异常处理的内部， 因为当线程处于 阻塞状态的时候， interrupt中断就是以异常的方式通知的
                    // 如果不加 break的话，那么这个循环不会终止，因为在以异常的方式通知之后， 中断的标志位就被清除了
                }
            }
            System.out.println(Thread.currentThread().getName()
                    + ": 啊！险些误了大事");
        }
    }


    public static void main5(String[] args) throws InterruptedException {
        MyRunnable1 target = new MyRunnable1();
        Thread thread = new Thread(target, "李四");
        System.out.println(Thread.currentThread().getName()+"让李四开始转账");
        thread.start();
        Thread.sleep(1000*5);
        System.out.println(Thread.currentThread().getName()
                + ": 老板来电话了，得赶紧通知李四对方是个骗子！");
        thread.interrupt();
        //target.isQuit = true;

    }


    public static void main1(String[] args) {
        Thread t1 = new Thread(()->{
            for (int i = 0; i < 10; i++) {
                System.out.println(Thread.currentThread().getName() + ":我还活着");
                try{
                    Thread.sleep(1*1000);
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
            }
            System.out.println(Thread.currentThread().getName() + ": 我即将死去");
        });

        System.out.println(t1.getName());
        System.out.println(Thread.currentThread().getName());
        System.out.println(t1.getId());
        System.out.println(t1.getState());
        System.out.println(t1.getPriority());
        System.out.println(t1.isAlive());
        System.out.println(t1.isInterrupted());
        System.out.println(t1.isDaemon());
        t1.start();
        System.out.println(t1.getName());
        System.out.println(Thread.currentThread().getName());
        System.out.println(t1.getId());
        System.out.println(t1.getState());
        System.out.println(t1.getPriority());
        System.out.println(t1.isAlive());
        System.out.println(t1.isInterrupted());
        System.out.println(t1.isDaemon());

//        while(t1.isAlive()){
//            System.out.println(t1.getName()+"：状态："+ t1.getState());
//            System.out.println(Thread.currentThread().getName()+"：状态："+ Thread.currentThread().getState());
//        }


    }






}
